package com.time.workshop.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.time.workshop.API;
import com.time.workshop.App;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.LaoBanDialog;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.OnClickOKListener;
/**
 * 同城、陪跑、遛宠物
 * @author wangfeng
 *
 */
public class CustomMadeActivity extends SuperActivity implements OnClickListener{
	@V
	private ImageView img_login_back;
	@V
	private TextView cancelTextView,sureTextView,title,timeTextView;
	@V
	private EditText maoshu,nameEditText,phoneEditText;
	@V
	private View phone_View,name_View,time_View;
	@V
	private RelativeLayout rl_price;
	
	private String content,name,phone,type,timeString;

	private DatePicker datePicker;  
	private TimePicker timePicker;  
	private AlertDialog dialog;  
	public Calendar time = Calendar.getInstance(Locale.CHINA); 
	private boolean processFlag = true;
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_custommade);
		init();
	}

	private void init() {
		Injector.getInstance().inject(this);
		img_login_back.setOnClickListener(this);
		cancelTextView.setOnClickListener(this);
		sureTextView.setOnClickListener(this);
		timeTextView.setOnClickListener(this);
		if (App.getInstance().type == BaseConstant.TONGCHENG) {
			title.setText(getResources().getString(R.string.custommade));
			type = "TCDD";
			name_View.setVisibility(View.VISIBLE);
			nameEditText.setVisibility(View.VISIBLE);
			phoneEditText.setVisibility(View.VISIBLE);
			time_View.setVisibility(View.VISIBLE);
			timeTextView.setVisibility(View.VISIBLE);
			rl_price.setVisibility(View.GONE);
		}else if (App.getInstance().type == BaseConstant.CHONGWU) {
			title.setText(getResources().getString(R.string.chongwu));
			type = "LCWDD";
		}else if (App.getInstance().type == BaseConstant.PEIPAO) {
			title.setText(getResources().getString(R.string.wantpeipao));
			type = "PPDD";
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_login_back:
			CommonUtils.hide(this);
			back();
			break;
		case R.id.cancelTextView:
			CommonUtils.hide(this);
			back();
			break;
		case R.id.timeTextView:
			if (processFlag == true) {
				dateTimePickerDialog();  
				processFlag = false;
				new TimeThread().start(); 
			}

			break;
		case R.id.sureTextView:
			content = maoshu.getText().toString().trim();
			name = nameEditText.getText().toString().trim();
			phone = phoneEditText.getText().toString().trim();
			timeString = timeTextView.getText().toString().trim();
			if ("".equals(CommonUtils.isNull(name))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_name));
				return;
			}
			if (!CommonUtils.isMobile(phone)) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_phone));
				return;
			}
			if ("".equals(CommonUtils.isNull(content))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_need));
				return;
			}
			if (App.getInstance().type == BaseConstant.TONGCHENG) {//同城
				if ("".equals(CommonUtils.isNull(timeString))) {
					NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_time));
					return;
				}
			}
			showConfirmDialog();
			break;
		default:
			break;
		}
	}

	private LaoBanDialog dlg;
	/**
	 * 显示  Dailog 
	 */
	private void showConfirmDialog() {

		dlg = new LaoBanDialog(CustomMadeActivity.this);
		if (dlg.isShowing()) {
			dlg.dismiss();
			return;
		}
		dlg.onCreate();
		dlg.setOnClickOK(new OnClickOKListener() {

			@Override
			public void setOnClickOK() {
				//				Intent in = new Intent(CustomMadeActivity.this,ConfirmOrderActivity.class);
				//				in.putExtra("name", name);
				//				in.putExtra("content", content);
				//				in.putExtra("phone", phone);
				//				startActivity(in, true);
				//				dlg.dismiss();
				dlg.dismiss();
				RequestOrder requestOrder = new RequestOrder();
				requestOrder.ORDER_TYPE = type;
				requestOrder.contactor_person = name;
				requestOrder.contactor_phone = phone;
				requestOrder.remark = content;
				requestOrder.reservation_time = timeString;
				API.submitOrder(requestOrder, CustomMadeActivity.this);
			}
		});
	}

	//选择时间
	public AlertDialog dateTimePickerDialog(){  
		LinearLayout dateTimeLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.date_time_picker, null);  
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.DatePicker);  
		timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.TimePicker);  
		timePicker.setIs24HourView(true);  

		OnTimeChangedListener timeListener= new OnTimeChangedListener() {  
			@Override  
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
				time.set(Calendar.HOUR_OF_DAY, hourOfDay);  
				time.set(Calendar.MINUTE, minute);  

			}  
		};  

		timePicker.setOnTimeChangedListener(timeListener);  
		OnDateChangedListener dateListener = new OnDateChangedListener() {  

			@Override  
			public void onDateChanged(DatePicker view, int year, int monthOfYear,  
					int dayOfMonth) {  
				time.set(Calendar.YEAR, year);  
				time.set(Calendar.MONTH, monthOfYear);  
				time.set(Calendar.DAY_OF_MONTH, dayOfMonth);  
			}  
		};  

		datePicker.init(time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH), dateListener);  
		timePicker.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));  
		timePicker.setCurrentMinute(time.get(Calendar.MINUTE));  

		dialog = new AlertDialog.Builder(this).setTitle("设置日期时间").setView(dateTimeLayout)  
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

						time.set(Calendar.YEAR, datePicker.getYear());  
						time.set(Calendar.MONTH, datePicker.getMonth());  
						time.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());  
						time.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());  
						time.set(Calendar.MINUTE, timePicker.getCurrentMinute());  
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
						timeTextView.setText(format.format(time.getTime()));
					}  
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

					}  
				}).show();  
		return dialog;  
	} 

	private class TimeThread extends Thread {    
		public void run() {  
			try {  
				Thread.sleep(2000);  
				processFlag = true;  
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
		}  
	} 

}
